import { useIntl, useModel } from '@umijs/max';
import { Button, message } from 'antd';
import FormattedMessageCustom from '../FormattedMessageCustom/FormattedMessageCustom';
type IParams = {
    toMobile?: string;
};
const ImBtn: React.FC<IParams> = (props) => {
    const { createC2CConversation } = useModel('useTencentIm');
    const { initialState } = useModel('@@initialState');
    const intl = useIntl();
    function communicate() {
        if (!props.toMobile) {
            return message.error('参数错误');
        }
        if (initialState?.user) {
            if (props.toMobile === initialState.user.mobile) {
                message.error(intl.formatMessage({ id: '不能与自己聊天' }));
                return false;
            }
            
            console.log(localStorage.getItem('level'))

            if(initialState.user.level==0){
                message.error('聊天共同功能未开启');
                return false;
            }

            createC2CConversation(
                props.toMobile,
                initialState.user.mobile,
            ).catch(() => {
                message.error('聊天共同功能未开启');
            });
        }
    }
    return (
        <Button type="primary" ghost className="warning" onClick={communicate}>
            <FormattedMessageCustom id="即时沟通"></FormattedMessageCustom>
        </Button>
    );
};
export default ImBtn;
